package com.ofps.flow.service.impl;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ofps.common.core.domain.entity.SysDept;
import com.ofps.common.core.domain.entity.SysUser;
import com.ofps.common.utils.StringUtils;
import com.ofps.flow.domain.Dto.FlowInfoDto;
import com.ofps.flow.domain.Dto.FlowInfoResDto;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.domain.MyForm;
import com.ofps.system.mapper.SysDeptMapper;
import com.ofps.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ofps.flow.mapper.EnterFlowInfoMapper;
import com.ofps.flow.domain.EnterFlowInfo;
import com.ofps.flow.service.IEnterFlowInfoService;

import javax.annotation.Resource;

/**
 * 流程Service业务层处理
 *
 * @author ofps
 * @date 2022-06-01
 */
@Service
public class EnterFlowInfoServiceImpl extends ServiceImpl<EnterFlowInfoMapper, EnterFlowInfo> implements IEnterFlowInfoService {
    @Resource
    private EnterFlowInfoMapper enterFlowInfoMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询流程列表
     *
     * @param enterFlowInfo 流程
     * @return 流程
     */
    @Override
    public List<EnterFlowInfo> selectEnterFlowInfoList(EnterFlowInfo enterFlowInfo) {
        return baseMapper.selectEnterFlowInfoList(enterFlowInfo);
    }

    @Override
    public List<EnterFlowInfo> queryWithDeptId(Map<String, Object> body) {
        QueryWrapper<EnterFlowInfo> enterFlowInfoQueryWrapper = new QueryWrapper<>();
        enterFlowInfoQueryWrapper.eq("dept_id", body.get("departmentId").toString());
        List<EnterFlowInfo> enterFlowInfos = enterFlowInfoMapper.selectList(enterFlowInfoQueryWrapper);
        System.out.println(enterFlowInfoQueryWrapper);
        if (null != enterFlowInfos)
            return enterFlowInfos;
        else
            return null;
    }

    @Override
    public R myList(FlowInfoDto flowInfo) {
        QueryWrapper<EnterFlowInfo> queryWrapper = new QueryWrapper<>();
        System.out.println(flowInfo);
        //根据用户名 查询用户id
        //匹配用户名的用户id数组
        List<Long> userIds = new LinkedList<>();
        if (StringUtils.isNotNull(flowInfo.getUserName())) {
            userIds.addAll(sysUserMapper.selectIdsByUserName(flowInfo.getUserName()));
        }

        queryWrapper
                .eq(StringUtils.isNotNull(flowInfo.getUserName()) && userIds.isEmpty(), "user_id", -1L)
                //用户名
                .in(!userIds.isEmpty(), "user_id", userIds)
                //部门id
                .in(null != flowInfo.getDeptIds(), "dept_id", flowInfo.getDeptIds())
                //流程名称
                .like(StringUtils.isNotNull(flowInfo.getFlowName()), "name", flowInfo.getFlowName())
                //flow_en
                .like(StringUtils.isNotNull(flowInfo.getFlowIden()), "flow_iden", flowInfo.getFlowIden())
                //status
                .eq(StringUtils.isNotNull(flowInfo.getFlowStatus()), "status", flowInfo.getFlowStatus())
                //创建时间
                .apply(flowInfo.getDateRange() != null && StringUtils.isNotNull(flowInfo.getDateRange().get("beginTime")),
                        "date_format (create_time,'%Y-%m-%d') >= date_format('" + flowInfo.getDateRange().get("beginTime") + "','%Y-%m-%d')")
                .apply(flowInfo.getDateRange() != null && StringUtils.isNotNull(flowInfo.getDateRange().get("endTime")),
                        "date_format (create_time,'%Y-%m-%d') <= date_format('" + flowInfo.getDateRange().get("endTime") + "','%Y-%m-%d')");

        System.out.println(queryWrapper);
        //查询
        Page<EnterFlowInfo> flowInfoPage = enterFlowInfoMapper.selectPage(
                new Page<>(flowInfo.getPageNum(), flowInfo.getPageSize()), queryWrapper);

        //总数
        Long counts = enterFlowInfoMapper.selectCount(queryWrapper);
        if (flowInfoPage == null) return R.error("没有符合的流程表单！");
        //返回信息 + username nickname deptName
        List<EnterFlowInfo> records = flowInfoPage.getRecords();
        List<FlowInfoResDto> resMap = new LinkedList<>();
        for (EnterFlowInfo flow : records) {
            SysUser sysUser = sysUserMapper.selectUserById(flow.getUserId());
            SysDept sysDept = sysDeptMapper.selectDeptById(flow.getDeptId());
            FlowInfoResDto flowInfoResDto = new FlowInfoResDto()
                    .setFlowInfo(flow)
                    .setUserName(sysUser.getUserName())
                    .setNickName(sysUser.getNickName())
                    .setDeptName(sysDept.getDeptName());
            resMap.add(flowInfoResDto);
        }
        return R.ok("查询成功").put("flowInfo", resMap).put("counts", counts);
    }

    @Override
    public Boolean insert(EnterFlowInfo enterFlowInfo) {
        boolean flow_iden = enterFlowInfoMapper.exists(
                new QueryWrapper<EnterFlowInfo>()
                        .eq("flow_iden", enterFlowInfo.getFlowIden()));
        if (flow_iden)
            return true;
        else {
            enterFlowInfoMapper.insert(enterFlowInfo);
            return false;
        }
    }


}
